package daos;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Session entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see daos.Session
 * @author MyEclipse Persistence Tools
 */

public class SessionDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(SessionDAO.class);
	// property constants
	public static final String SESSION_DATE = "sessionDate";
	public static final String SEATS = "seats";
	public static final String PRICE = "price";

	protected void initDao() {
		// do nothing
	}

	public void save(Session transientInstance) {
		log.debug("saving Session instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Session persistentInstance) {
		log.debug("deleting Session instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Session findById(java.lang.Integer id) {
		log.debug("getting Session instance with id: " + id);
		try {
			Session instance = (Session) getHibernateTemplate().get(
					"daos.Session", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Session instance) {
		log.debug("finding Session instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Session instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Session as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findBySessionDate(Object sessionDate) {
		return findByProperty(SESSION_DATE, sessionDate);
	}

	public List findBySeats(Object seats) {
		return findByProperty(SEATS, seats);
	}

	public List findByPrice(Object price) {
		return findByProperty(PRICE, price);
	}

	public List findAll() {
		log.debug("finding all Session instances");
		try {
			String queryString = "from Session";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Session merge(Session detachedInstance) {
		log.debug("merging Session instance");
		try {
			Session result = (Session) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Session instance) {
		log.debug("attaching dirty Session instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Session instance) {
		log.debug("attaching clean Session instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static SessionDAO getFromApplicationContext(ApplicationContext ctx) {
		return (SessionDAO) ctx.getBean("SessionDAO");
	}
		
	/******************************* added by Sheila *******************************/
	public List<Session> findByMovie(int movieId) {
		String hql = "select ses from Session ses inner join fetch ses.movie m where m.movieId=:argMovieId";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		
		query.setParameter("argMovieId", new Integer(movieId));
		List<Session> result = query.list();
		return result;
	}
	
	public void deleteById(Integer sessionId) {
		String sql = "delete from Orders where sessionId=:sesId";
		String hql = "delete from Session where sessionId=:sesId";
		SQLQuery sqlQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().createSQLQuery(sql);
		Query hqlQuery = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		
		sqlQuery.setParameter("sesId", sessionId);
		sqlQuery.executeUpdate();
		
		hqlQuery.setParameter("sesId", sessionId);
		hqlQuery.executeUpdate();
	}
	
	public void updateSession(Session session) {
		String hql = "update Session set sessionDate=:sesDate," +
				" seats=:sesSeats, price=:sesPrice, hall=:sesHall" +
				" where sessionId=:sesId";
		Query query = getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		
		query.setParameter("sesDate", session.getSessionDate());
		query.setParameter("sesSeats", session.getSeats());
		query.setParameter("sesPrice", session.getPrice());
		query.setParameter("sesHall", session.getHall());
		query.setParameter("sesId", session.getSessionId());
		query.executeUpdate();
	}
	/******************************* ends Sheila ***********************************/
	/**
	 * @author SerenaMing
	 * @param sessionId
	 * @param sessionSeats
	 */
	public void updateSessionSeats(int sessionId, String sessionSeats){
		Session session = findById(sessionId);
		session.setSeats(sessionSeats);
		try {
			getHibernateTemplate().update(session);
			log.debug("update seats successful");
		}catch (RuntimeException re) {
			log.error("update seats failed", re);
			throw re;
		}
	}
	
}